﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Rcompany.EntityFrameworkCore;
using Rcompany.Model;
using Rcompany.PaymentManagementDTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;


namespace Rcompany.PaymentManagements
{
    /// <summary>
    /// 付款管理
    /// </summary>
    public class PaymentManagementStore:ApplicationService,IPaymentManagements
    {
        public readonly IRepository<PaymentManagement, Guid>  payments;
        public PaymentManagementStore(IRepository<PaymentManagement, Guid> _payments)
        {
            payments = _payments;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("Add")]
        public async Task<PaymentManagementDto> Add(CreateUpdatePaymentDto m)
        {
            var create = await payments.InsertAsync(ObjectMapper.Map<CreateUpdatePaymentDto, PaymentManagement>(m));
            return ObjectMapper.Map<PaymentManagement,PaymentManagementDto>(create);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task Del(Guid id)
        {
            await payments.DeleteAsync(id);
        }

        /// <summary>
        /// 付款管理显示
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("Show")]
        public async  Task<List<CreateUpdatePaymentDto>> Show(string name)
        {
            if (name==null)
            {
                List<PaymentManagement> managements = await payments.ToListAsync();
                List<CreateUpdatePaymentDto> dtos = ObjectMapper.Map<List<PaymentManagement>, List<CreateUpdatePaymentDto>>(managements);
                return dtos;
            }
            else
            {

                List<PaymentManagement> managements = await payments.Where(x => x.Title.Equals(name)).ToListAsync();
                List<CreateUpdatePaymentDto> dtos = ObjectMapper.Map<List<PaymentManagement>, List<CreateUpdatePaymentDto>>(managements);
                return dtos;
            }
        }
        [HttpGet]
        [Route("Show2")]
        public async Task<List<PaymentManagementDto>> Show2()
        {
            List<PaymentManagement> managements = await payments.ToListAsync();
            List<PaymentManagementDto> dtos = ObjectMapper.Map<List<PaymentManagement>, List<PaymentManagementDto>>(managements);         
            return dtos;
        }
        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("Edit")]
        public async Task<PaymentManagementDto> Edit(Guid id)
        {
            var date = await payments.GetAsync(id);
            return ObjectMapper.Map<PaymentManagement, PaymentManagementDto>(date);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("Upt")]
        public async Task<int> Upt(PaymentManagementDto m)
        {
            PaymentManagement ment = await payments.GetAsync(m.Id);
            ment.DatePayment = m.DatePayment;
            ment.CreationTime = m.CreationTime;
            ment.Status = m.Status;
            ment.Title = m.Title;
            ment.Describe = m.Describe;
            ment.Amount = m.Amount;
            ment.MethodPayment = m.MethodPayment;
            ment.Payobject = m.Payobject;
            ment.OpenAccount = m.OpenAccount;
            ment.Account = m.Account;
            ment.ApplyPeople = m.ApplyPeople;
            ment.Note = m.Note;
            ment.Approver = m.Approver;
            ment.Attachment = m.Attachment;
            ment.MyProperty = m.MyProperty;
            return 1;
        }
    }
}
